package jupp;

import java.io.Serializable;
import java.util.Scanner;

public class AddressJupp
{
	public static class Address implements Serializable
	{
		public final static long serialVersionUID = 42l;
		protected String name;
		protected String web;
		protected String email;
		protected String phone;
		public Address(String n, String w, String e, String p)
		{
			name = n;
			web = w;
			email = e;
			phone = p;
		}
		public String getName()
		{ return name; }
		public String toString()
		{
			StringBuilder sb = new StringBuilder();
			sb.append(name);
			if (web != null)
				sb.append(" ").append(web);
			if (email != null)
				sb.append(" ").append(email);
			if (phone != null)
				sb.append(" phone:").append(phone);
			return sb.toString();
		}
	}
	public static void main(String[] args) throws Exception
	{
		JuppMap<Address> addresses = new JuppMap<Address>("address.jupp");

		Scanner scanner = new Scanner(System.in);
		while (true)
		{
			System.out.println("(F)ind, (L)ist, (N)ew, (D)elete or (Q)uit");
			String choice = scanner.nextLine();
			switch (choice.toUpperCase().charAt(0))
			{
				case 'F':
					System.out.println("Name to find?");
					String name = scanner.nextLine().toLowerCase();
					for (String key : addresses.getKeys())
						if (key.toLowerCase().contains(name))
							System.out.println("Found: " + addresses.get(key));
					System.out.println("Find complete");
					break;
				case 'L':
					System.out.println("List...");
					for (String key : addresses.getKeys())
						System.out.println(key + " -> " + addresses.get(key));
					System.out.println("List complete");
					break;
				case 'N':
					System.out.println("Name?");
					name = scanner.nextLine();
					System.out.println("Homepage?");
					String w = scanner.nextLine();
					System.out.println("Email?");
					String e = scanner.nextLine();
					System.out.println("Phone?");
					String p = scanner.nextLine();
					addresses.put(name, new Address(name
					 , w.length() > 0?w:null
					 , e.length() > 0?e:null
					 , p.length() > 0?p:null));
					break;
				case 'D':
					System.out.println("Name to remove?");
					name = scanner.nextLine();
					Address removed = addresses.remove(name);
					System.out.println("Removed: " + removed);
					break;
				case 'Q':
					System.out.println("Bye!");
					System.exit(0);
					break;
				default:
					System.out.println("Something wrong with '" + choice + "'");
			} // switch user choice
		}
	}
}
